﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PlantsVsZombies
{
    public class MapManager : EntityManager
    {
        #region "Varible"
        private Map _wel;
        private LoadBar2D _loadBar2D;
        private Sprite _start;
        private Sprite _loading;
        private Map _background;
        private Sprite _logo;
        private Map _garden;

        

        private const float Acceleration = 5.0f; // gia toc chuyen dong.
        private float _t = 0; // thoi gian chuyen dong.
        SpriteFont font;

        #endregion
        public override void LoadContent(ContentManager contentManager)
        {
            LoadWelcomeScreen(contentManager);
            LoadPlayingScreen(contentManager);
            
        }

        

        private void LoadWelcomeScreen(ContentManager contentManager)
        {
            _wel = new Map(
                contentManager,
                @"Image/PlantsVsZombies", 
                new Vector2(804, 606),
                Vector2.Zero);

            _loadBar2D = new LoadBar2D(new Vector2(200, 620));
            _loadBar2D.LoadContent(contentManager);



            int nStart = 2;
            String[] strStart = new string[nStart];
            strStart[0] = @"Image\ClicktoStart\click_to_start";
            strStart[1] = @"Image\ClicktoStart\click_to_start_1";
            _start = new Sprite(
                contentManager,
                strStart,
                nStart,
                new Vector2(_loadBar2D.LoadBarPosition.X + 120, _loadBar2D.LoadBarPosition.Y + 90),
                Vector2.Zero); //{State = 0};

            _background = new Map(
                contentManager,
                @"Image/Menu/bg1",
                new Vector2(804, 603),
                Vector2.Zero);

            _logo = new Sprite(
                contentManager,
                @"Image/LogoWallpaper",
                new Vector2(52, -40),
                Vector2.Zero);

            _loading = new Sprite(
                contentManager,
                @"Image/loading",
                new Vector2(200,620), 
                new Vector2(160,40));

            //////
            font = contentManager.Load<SpriteFont>("SpriteFont1");
        }
        private void LoadPlayingScreen(ContentManager contentManager)
        {
            _garden = new Map(contentManager, 
                                  @"Image\Garden\map", 
                                  Vector2.Zero,
                                  Vector2.Zero); 
          
            
          
        }
        


        public override void Update(ref GameTime gameTime, ref GameStates gameState)
        {
            //base.Update(gameTime, gameStates);
            switch (gameState)
            {
                case GameStates.WelcomeScreen:
                    {
                        UpdateWelcomeScreen(gameTime, ref gameState);
                        break;
                    }
                case GameStates.SelectScreen:
                    {
                       
                        break;
                    }

                case GameStates.Playing:
                    {
                        UpdatePlayingScreen(gameTime, ref gameState);
                        break;
                    }
                 
            }
            if (Math.Abs(_t - float.MaxValue) < 1.0f)
                _t = 0;
            else
                _t += 0.01f;
        }

       

        private void UpdateWelcomeScreen(GameTime gameTime, ref GameStates gameState)
        {
  
            Vector2 posLoadBar = _loadBar2D.LoadBarPosition;
            if (posLoadBar.Y >= 460)
                posLoadBar.Y -= (float) (0.5*Acceleration*_t*_t);
            else
            {
                _loadBar2D.Progress += 0.005f;
                Vector2 posStart = _start.Position;
                posStart.Y = _loadBar2D.LoadBarPosition.Y + 90;
                _start.Position = posStart;
                _loading.Position = posStart;
            }
            _loadBar2D.LoadBarPosition = posLoadBar;
            _loadBar2D.Update(gameTime);
           
            Vector2 posLogo = _logo.Position;
            if (posLogo.Y < 14)
                posLogo.Y += _t + (float) (0.5*Acceleration*_t*_t);

            _logo.Position = posLogo;

            _logo.Update(gameTime);

           
            MouseState mouse = Mouse.GetState();

            #region "Xu ly su kien chuot"

            if (((mouse.LeftButton == ButtonState.Pressed)
                 && _start.IsClicked(new Vector2(mouse.X, mouse.Y)))
                 && (Math.Abs(_loadBar2D.Progress - 1) <= 0.01))
            {
                gameState = GameStates.SelectScreen;
            }
            if (_start.IsClicked(new Vector2(mouse.X, mouse.Y)))
                _start.State = 0;
            else
                _start.State = 1;
            _start.Update(gameTime);

            #endregion
        }
        private void UpdatePlayingScreen(GameTime gameTime, ref GameStates gameState)
        {

        }


        public override void Draw(SpriteBatch spriteBatch, GameStates gameState)
        {
          switch (gameState)
            {

                case GameStates.WelcomeScreen:
                    {
                        _wel.Draw(spriteBatch);
                        _loadBar2D.Draw(spriteBatch);
                        if (Math.Abs(_loadBar2D.Progress - 1) <= 0.001)
                            _start.Draw(spriteBatch);
                        else
                        {
                            _loading.Draw(spriteBatch);
                        }
                        _logo.Draw(spriteBatch);
                        break;
                    }
                case GameStates.SelectScreen:
                    {
                        _background.Draw(spriteBatch);                       
                        break;
                    }
                case GameStates.Playing:
                    {
                        _garden.Draw(spriteBatch);
                       break;
                    }
               
            }
        }
      
    } 
}
